package cn.ctodb.divination.web.rest;

import com.codahale.metrics.annotation.Timed;
import cn.ctodb.divination.service.SancaiService;
import cn.ctodb.divination.web.rest.util.HeaderUtil;
import cn.ctodb.divination.service.dto.SancaiDTO;
import io.github.jhipster.web.util.ResponseUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.net.URI;
import java.net.URISyntaxException;
import java.util.LinkedList;
import java.util.List;
import java.util.Optional;
import java.util.stream.Collectors;
import java.util.stream.StreamSupport;

import static org.elasticsearch.index.query.QueryBuilders.*;

/**
 * REST controller for managing Sancai.
 */
@RestController
@RequestMapping("/api")
public class SancaiResource {

    private final Logger log = LoggerFactory.getLogger(SancaiResource.class);

    private static final String ENTITY_NAME = "sancai";
        
    private final SancaiService sancaiService;

    public SancaiResource(SancaiService sancaiService) {
        this.sancaiService = sancaiService;
    }

    /**
     * POST  /sancais : Create a new sancai.
     *
     * @param sancaiDTO the sancaiDTO to create
     * @return the ResponseEntity with status 201 (Created) and with body the new sancaiDTO, or with status 400 (Bad Request) if the sancai has already an ID
     * @throws URISyntaxException if the Location URI syntax is incorrect
     */
    @PostMapping("/sancais")
    @Timed
    public ResponseEntity<SancaiDTO> createSancai(@RequestBody SancaiDTO sancaiDTO) throws URISyntaxException {
        log.debug("REST request to save Sancai : {}", sancaiDTO);
        if (sancaiDTO.getId() != null) {
            return ResponseEntity.badRequest().headers(HeaderUtil.createFailureAlert(ENTITY_NAME, "idexists", "A new sancai cannot already have an ID")).body(null);
        }
        SancaiDTO result = sancaiService.save(sancaiDTO);
        return ResponseEntity.created(new URI("/api/sancais/" + result.getId()))
            .headers(HeaderUtil.createEntityCreationAlert(ENTITY_NAME, result.getId().toString()))
            .body(result);
    }

    /**
     * PUT  /sancais : Updates an existing sancai.
     *
     * @param sancaiDTO the sancaiDTO to update
     * @return the ResponseEntity with status 200 (OK) and with body the updated sancaiDTO,
     * or with status 400 (Bad Request) if the sancaiDTO is not valid,
     * or with status 500 (Internal Server Error) if the sancaiDTO couldnt be updated
     * @throws URISyntaxException if the Location URI syntax is incorrect
     */
    @PutMapping("/sancais")
    @Timed
    public ResponseEntity<SancaiDTO> updateSancai(@RequestBody SancaiDTO sancaiDTO) throws URISyntaxException {
        log.debug("REST request to update Sancai : {}", sancaiDTO);
        if (sancaiDTO.getId() == null) {
            return createSancai(sancaiDTO);
        }
        SancaiDTO result = sancaiService.save(sancaiDTO);
        return ResponseEntity.ok()
            .headers(HeaderUtil.createEntityUpdateAlert(ENTITY_NAME, sancaiDTO.getId().toString()))
            .body(result);
    }

    /**
     * GET  /sancais : get all the sancais.
     *
     * @return the ResponseEntity with status 200 (OK) and the list of sancais in body
     */
    @GetMapping("/sancais")
    @Timed
    public List<SancaiDTO> getAllSancais() {
        log.debug("REST request to get all Sancais");
        return sancaiService.findAll();
    }

    /**
     * GET  /sancais/:id : get the "id" sancai.
     *
     * @param id the id of the sancaiDTO to retrieve
     * @return the ResponseEntity with status 200 (OK) and with body the sancaiDTO, or with status 404 (Not Found)
     */
    @GetMapping("/sancais/{id}")
    @Timed
    public ResponseEntity<SancaiDTO> getSancai(@PathVariable Long id) {
        log.debug("REST request to get Sancai : {}", id);
        SancaiDTO sancaiDTO = sancaiService.findOne(id);
        return ResponseUtil.wrapOrNotFound(Optional.ofNullable(sancaiDTO));
    }

    /**
     * DELETE  /sancais/:id : delete the "id" sancai.
     *
     * @param id the id of the sancaiDTO to delete
     * @return the ResponseEntity with status 200 (OK)
     */
    @DeleteMapping("/sancais/{id}")
    @Timed
    public ResponseEntity<Void> deleteSancai(@PathVariable Long id) {
        log.debug("REST request to delete Sancai : {}", id);
        sancaiService.delete(id);
        return ResponseEntity.ok().headers(HeaderUtil.createEntityDeletionAlert(ENTITY_NAME, id.toString())).build();
    }

    /**
     * SEARCH  /_search/sancais?query=:query : search for the sancai corresponding
     * to the query.
     *
     * @param query the query of the sancai search 
     * @return the result of the search
     */
    @GetMapping("/_search/sancais")
    @Timed
    public List<SancaiDTO> searchSancais(@RequestParam String query) {
        log.debug("REST request to search Sancais for query {}", query);
        return sancaiService.search(query);
    }


}
